﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using MEF_Test.Bricks;
using MEF_Test.Bricks.Actions;
using MEF_Test.Contracts;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ModuleOne;
using Moq;
using OpenAction;
using ShareBrowserAction;


namespace MEF_Test.Module.ModuleOne.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class ActionsTests
    {
        private Mock<IActionsManager> _actionManagerMock;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestInitialize]
        public void IoCInitialize()
        {
            _actionManagerMock = new Mock<IActionsManager>();
            _actionManagerMock.Setup(o => o.ExportedActions).Returns(
                new ObservableCollection<Lazy<INodeAction, IDictionary<string, object>>>());
            IoC.Current.RegisterInstance(_actionManagerMock.Object);

        }

        [TestMethod]
        public void ShareActionContextProviderSelectionNominalTest()
        {
            // exported actios to inject.
            var exportedActions = new ObservableCollection<Lazy<INodeAction, IDictionary<string, object>>>();

            // the share action

            // The shareAction Provider : responsible to do the actual work on an open command.
            var shareActionProviderMock = new Mock<IShareActionContextProvider>();

            // Let's configure its "Supports" method
            shareActionProviderMock.Setup(o => o.Supports(It.IsAny<IBrowserNodeViewModel>())).Returns(true);

            // we grab the actual object.
            IShareActionContextProvider shareActionProvider = shareActionProviderMock.Object;

            Lazy<INodeAction, IDictionary<string, object>> openAction = new Lazy<INodeAction, IDictionary<string, object>>(
                () => new ShareAction.ShareAction(new[] { new Lazy<IShareActionContextProvider>(() => shareActionProvider) })
                , new Dictionary<string, object> { { "Action", "Open" } });

            exportedActions.Add(openAction);

            // Let's configure the ActionsManager to return our mocked exportedActions
            _actionManagerMock.Setup(o => o.ExportedActions).Returns(exportedActions);

            // Create a new BrowserNode
            // Test whether its actions are set allright


            var bn = LocalDiskFileHandler.CreateBrowserNode("C:\\");
            Assert.IsTrue(bn.SupportedActions.FirstOrDefault() != null);
        }

        [TestMethod]
        public void OpenActionContextProviderSelectionNominalTest()
        {
            // exported actios to inject.
            var exportedActions = new ObservableCollection<Lazy<INodeAction, IDictionary<string, object>>>();

            // the open action

            // The openAction Provider : responsible to do the actual work on an open command.
            var openActionProviderMock = new Mock<IOpenActionContextProvider>();

            // Let's configure its "Supports" method
            openActionProviderMock.Setup(o => o.Supports(It.IsAny<IBrowserNodeViewModel>())).Returns(true);

            // we grab the actual object.
            IOpenActionContextProvider openActionProvider = openActionProviderMock.Object;

            Lazy<INodeAction, IDictionary<string, object>> openAction = new Lazy<INodeAction, IDictionary<string, object>>(
                () => new OpenAction.OpenAction(new[] { new Lazy<IOpenActionContextProvider>(() => openActionProvider) })
                , new Dictionary<string, object> { { "Action", "Open" } });

            exportedActions.Add(openAction);

            // Let's configure the ActionsManager to return our mocked exportedActions
            _actionManagerMock.Setup(o => o.ExportedActions).Returns(exportedActions);

            // Create a new BrowserNode
            // Test whether its actions are set allright


            var bn = LocalDiskFileHandler.CreateBrowserNode("C:\\");
            Assert.IsTrue(bn.SupportedActions.FirstOrDefault() != null);
        }

        [TestMethod]
        public void OpenActionContextProviderSelectionWithoutCompatibleActionTest()
        {
            // exported actios to inject.
            var exportedActions = new ObservableCollection<Lazy<INodeAction, IDictionary<string, object>>>();

            // the open action

            // The openAction Provider : responsible to do the actual work on an open command.
            var openActionProviderMock = new Mock<IOpenActionContextProvider>();

            // Let's configure its "Supports" method
            openActionProviderMock.Setup(o => o.Supports(It.IsAny<IBrowserNodeViewModel>())).Returns(true);


            Lazy<INodeAction, IDictionary<string, object>> openAction = new Lazy<INodeAction, IDictionary<string, object>>(
                () => new OpenAction.OpenAction(new Lazy<IOpenActionContextProvider>[0])
                , new Dictionary<string, object> { { "Action", "Open" } });

            exportedActions.Add(openAction);

            // Let's configure the ActionsManager to return our mocked exportedActions
            _actionManagerMock.Setup(o => o.ExportedActions).Returns(exportedActions);

            // Create a new BrowserNode
            // Test whether its actions are set allright


            var bn = LocalDiskFileHandler.CreateBrowserNode("C:\\");
            Assert.IsTrue(bn.SupportedActions.Count==0);
        }
    }
}
